package action.back;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import models.Module;
import models.Role;
import models.RoleModule;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.hibernate.criterion.DetachedCriteria;

import utils.JsonUtils;
import utils.StringHelper;

public class RoleAction extends DefaultAction {
	
	private Role role;
	
	public String index() {
		return "index";
	}
	
	@SuppressWarnings("unchecked")
	public void list() {
		
		DetachedCriteria dc = DetachedCriteria.forClass(Role.class);
	    page = getCommonService().findPage(dc, start, limit);
	    
	    Map<String, Object> jsonMap = new HashMap<String, Object>();
	    jsonMap.put("items",JsonUtils.toMapList(page.getItems(), "role"));
	    jsonMap.put("total", page.getTotal());
	    JsonConfig config = new JsonConfig();
	    
	    JSONObject jsonObject = JSONObject.fromObject(jsonMap,config);
		String jsonString = jsonObject.toString();
		renderJson(jsonString);
	}
	
	public void save() {
		getCommonService().saveOrUpdate(role);
		renderJson("{success:true}");
	}
	
	public void delete() {
		for(Integer id : ids){
			getCommonService().executeUpdate("delete UserRole where role.id = ?", id);
			getCommonService().executeUpdate("delete RoleModule where role.id = ?", id);
			getCommonService().executeUpdate("delete Role where id = ?", id);
		}
		renderJson("{success:true}");
	}
	
	@SuppressWarnings("unchecked")
	public void getModuleTrees() {
		Integer parent_id = Integer.parseInt(request.getParameter("parent_id"));
		Integer role_id = Integer.parseInt(request.getParameter("role_id"));
		
		List<Integer> moduleids = getCommonService().findAll("select module.id from RoleModule where role.id=?", role_id);
		
		List<Module> modules;
		if (parent_id == null || parent_id.intValue() == 0) {
			modules = getCommonService().findAll(Module.class, "position asc", "parent is null",null);
		} else {
			modules = getCommonService().findAll(Module.class, "position asc", "parent.id = ? ",new Object[]{parent_id});
		}
		
		List<Map> trees = new ArrayList<Map>();
		for (Module module : modules) {
			Map<String, Object> tree = new HashMap<String, Object>();
			tree.put("id", module.getId());
			tree.put("text", module.getName());
			tree.put("leaf", module.getSubModuleCount() + module.getSubFunctionCount() == 0 );
			tree.put("checked", moduleids.contains(module.getId()));
			if(StringHelper.parseBoolean(module.getIfFunc())){
				tree.put("iconCls", "admin-tool-func");
			}
			else{
				tree.put("iconCls", "admin-tool-module");
			}
			trees.add(tree);
		}
		
		JSONArray jsonObject = JSONArray.fromObject(trees);
		String jsonString = jsonObject.toString();
		renderJson(jsonString);
	}
	
	public void saveRoleModule() {
		
		getCommonService().executeUpdate("delete RoleModule where role=?", role);
		
		for(Integer module_id : ids){
			RoleModule rm = new RoleModule();
			rm.setRole(role);
			
			Module module = new Module();
			module.setId(module_id);
			rm.setModule(module);
			getCommonService().save(rm);
		}
		renderJson("{success:true}");
	}
	
	
	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}
}
